//获取应用实例
const app = getApp()
var that = this;
Page({
  data: {
    currentPage: "",
    pageSize: 8,
    list: [],
    id: [],
    title: [],
    reward: [],
    district: [],
    address: [],
    workTime: [],
    src: '/images/parttime/img_index.jpg',
    loading: false,
    chooseId: ""
  },

  onLoad: function() {
    this.refresh();
  },

  refresh: function(){
    var that = this;
    var newId = [];
    var newTitle = [];
    var newReward = [];
    var newCreateTime = [];
    var newAddress = [];
    var newDistrict = [];
    
    this.setData({
      currentPage: 1
    })

    wx.showNavigationBarLoading() //在标题栏中显示加载
    // 获取兼职列表
    wx.request({
      url: app.globalData.urlPath + 'getPartTimeList.action',
      data: {
        currentPage: that.data.currentPage,
        pageSize: that.data.pageSize,
      },
      success: function (res) {
        console.log(res.data);

        for (var i = 0; i < that.data.pageSize; i++) {
          if (!(res.data[i].id))
            break;
          newId = newId.concat(res.data[i].id);
          newTitle = newTitle.concat(res.data[i].title);
          newReward = newReward.concat(res.data[i].reward);
          newAddress = newAddress.concat(res.data[i].address);
          newDistrict = newDistrict.concat(res.data[i].district);
        // var CreateTime = [res.data[i].createTime];
          var createTime = that.timestampToTime(res.data[i].createTime);
          newCreateTime = newCreateTime.concat(createTime);
        }
        console.log(newCreateTime);
        that.setData({
          id: newId,
          title: newTitle,
          reward: newReward,
          createTime: newCreateTime,
          address: newAddress,
          district: newDistrict
        })
      }
    })
  },

  goToJobDetail: function(e) {
    var that = this;
    var id = e.currentTarget.dataset.id;
    wx.navigateTo({
      url: '../detail/detail?id=' + id,
    })
  },

  goToPublish: function() {
    wx.navigateTo({
      url: '../publish/publish',
    })
  },


  onPullDownRefresh: function() {
    wx.showNavigationBarLoading() //在标题栏中显示加载
    this.refresh();
    wx.hideNavigationBarLoading() //完成停止加载
    wx.stopPullDownRefresh() //停止下拉刷新
    wx.showToast({
      title: '加载成功',
      icon: 'none',
    })
  },

  onReachBottom: function() {
    var that = this;
    var data = that.data;
    var newId = data.id;
    var newTitle = data.title;
    var newReward = data.reward;
    var newCreateTime = data.createTime;
    var newAddress = data.address;
    var newDistrict = data.district;

    if (this.data.loading) {
      // console.log(this.data.loading);
      return;
    }
    this.setData({
      loading: true
    });
    updateRefreshIcon.call(this);


    wx.showNavigationBarLoading() //在标题栏中显示加载
    wx.request({
      url: app.globalData.urlPath + 'getPartTimeList.action',
      data: {
        currentPage: that.data.currentPage,
        pageSize: that.data.pageSize,
      },
      success: function(res) {
        var pageSize = that.data.pageSize;
        if(res.data.length < that.data.pageSize)
          pageSize = res.data.length

        if (res.data.length === 0){

          wx.showToast({
            title: '无更多数据',
            icon: 'none',
            })

          return
        }
        for (var i = 0; i < pageSize; i++) {
          
          newId = newId.concat(res.data[i].id);
          newTitle = newTitle.concat(res.data[i].title);
          newReward = newReward.concat(res.data[i].reward);
          var createTime = that.timestampToTime(res.data[i].createTime);
          newCreateTime = newCreateTime.concat(createTime);
          newAddress = newAddress.concat(res.data[i].address);
          newDistrict = newDistrict.concat(res.data[i].district);
        }
        setTimeout(() => {
          that.setData({
            loading: false,
            id: newId,
            title: newTitle,
            reward: newReward,
            createTime: newCreateTime,
            address: newAddress,
            district: newDistrict,
            currentPage : that.data.currentPage+1, //currentPage增加1
          })
        }, 2000)
      },
    })

    function updateRefreshIcon() {
      var deg = 0;
      console.log('旋转开始了.....')
      var animation = wx.createAnimation({
        duration: 1000
      });

      var timer = setInterval(() => { //按照指定的周期来调用函数
        if (!this.data.loading)
          clearInterval(timer);
        animation.rotateZ(deg).step(); //在Z轴旋转一个deg角度
        deg += 360;
        this.setData({
          refreshAnimation: animation.export()
        })
      }, 2000);
    }
  },

  timestampToTime: function (timestamp) {
    var date = new Date(timestamp);//时间戳为10位需*1000，时间戳为13位的话不需乘1000
    var Y = date.getFullYear() + '-';
    var M = (date.getMonth() + 1 < 10 ? '0' + (date.getMonth() + 1) : date.getMonth() + 1) + '-';
    var D = date.getDate() + ' ';
    // var h = date.getHours() + ':';
    // var m = date.getMinutes() + ':';
    // var s = date.getSeconds();
    return Y + M + D;
  }
})